package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserMapper;
import com.itheima.model.domain.User;
import com.itheima.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<User> selectUserWithPage(Integer page, Integer count) {
        PageHelper.startPage(page, count);
        List<User> userList = userMapper.selectUserWithPage();
        for (int i = 0; i < userList .size(); i++){
            User user=userList.get(i);
            user.setAuthority(userMapper.selectUserAuthority(user.getId()));
        }
        return new PageInfo<>(userList);
    }

    @Override
    public void deleteUserWithId(int id){
        userMapper.deleteUserWithId(id);
    }

    @Override
    public void updateAuthorityWithId(int id){userMapper.updateAuthorityWithId(id);}

    @Override
    public User selectUserWithId(int id){
        User user=userMapper.selectUserWithId(id);
        return user;
    }

    @Override
    public void updateUserWithId(User user){
        userMapper.updateUserWithId(user);
    }

    @Override
    public List<User> searchUsers(String keyword){
        List<User> userList = userMapper.findByUsernameContaining(keyword);
        for (int i = 0; i < userList .size(); i++){
            User user=userList.get(i);
            user.setAuthority(userMapper.selectUserAuthority(user.getId()));
        }
        return userList;
    }
}
